package com.gitee.neuray.security.mail.biz;

import com.gitee.neuray.security.common.biz.BaseBiz;
import com.gitee.neuray.security.common.util.EntityUtils;
import com.gitee.neuray.security.common.util.UUIDUtils;
import com.gitee.neuray.security.mail.mapper.MailReminderMapper;
import com.gitee.neuray.security.mail.util.EmailCommon;
import com.gitee.neuray.security.mail.vo.SysMailSourceVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.rowset.serial.SerialBlob;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class MailReminderBiz extends BaseBiz<MailReminderMapper, SysMailSourceVo> {

    @Override
    protected String getPageName() {
        return null;
    }

    @Autowired
    private MailReminderMapper mailReminderMapper;
    @Autowired
    private MailBiz mailBiz;

    private static final String template1 =
            "<!DOCTYPE html> \n" +
            "            <html> \n" +
            "            <head> \n" +
            "               <meta charset=\\utf-8\\> \n" +
            "               <title>网上报销审批提醒</title> \n" +
            "         <style type=\"text/css\">\n" +
            "          .box{ \n" +
            "            font-size: 20px; \n" +
            "            color:#000000; \n" +
            "            margin: 10px;\n" +
            "          }  \n" +
            "        </style>\n" +
            "</head>           \n" +
            "<body> \n" +
            " <div>\n" +
            "　　  <span class=\"box\" style=\"position:absolute; left:10px; top:5px\">\n" +
            "        <i>\n" +
            "        网上报销审批提醒：<br><br>\n" +
            "        &nbsp; &nbsp; &nbsp; &nbsp;请各位部门负责人于本月18号前完成报销审批流程，逾期审批的单据将会推延到下一周期报销。<br><br>\n" +
            "        具体审批方式如下：<br><br>\n" +
            "        &nbsp; &nbsp; &nbsp; &nbsp; 1、手机端扫码下图二维码，关注沈阳东睿科技，进入网上报销，点击报销审批<br><br>\n" +
            "        &nbsp; &nbsp; &nbsp; &nbsp; 2、电脑端访问地址：http://imp.neuray.cn<br><br>\n" +
            "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 账号为公司邮箱前缀，初始密码为123456，登录后请及时修改。<br><br>\n" +
            "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 例：邮箱地址zhangs@neuray.cn，登录账号zhangs<br><br>\n" +
            "        <img src=\"\" alt='img' >\n" +
            "        </i>\n" +
            "      </span>\n" +

            "  </div>\n" +
            " </body> \n" +
            "</html>";

    private static final String template2 =
            "<!DOCTYPE html> \n" +
                    "            <html> \n" +
                    "            <head> \n" +
                    "               <meta charset=\\utf-8\\> \n" +
                    "               <title>网上报销审批提醒</title> \n" +
                    "         <style type=\"text/css\">\n" +
                    "          .box{ \n" +
                    "            font-size: 20px; \n" +
                    "            color:#000000; \n" +
                    "            margin: 10px;\n" +
                    "          }  \n" +
                    "        </style>\n" +
                    "</head>           \n" +
                    "<body> \n" +
                    " <div>\n" +
                    "　　  <span class=\"box\" style=\"position:absolute; left:10px; top:5px\">\n" +
                    "        <i>\n" +
                    "        网上报销审批提醒：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;请各位部门负责人于本月18号前完成报销审批流程，本月的报销审批工作将在今天下班前截止，逾期审批的单据将会推延到下一周期报销。<br><br>\n" +
                    "        具体审批方式如下：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 1、手机端扫码下图二维码，关注沈阳东睿科技，进入网上报销，点击报销审批<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 2、电脑端访问地址：http://imp.neuray.cn<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 账号为公司邮箱前缀，初始密码为123456，登录后请及时修改。<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 例：邮箱地址zhangs@neuray.cn，登录账号zhangs<br><br>\n" +
                    "        <img src=\"\" alt='img' >\n" +
                    "        </i>\n" +
                    "      </span>\n" +

                    "  </div>\n" +
                    " </body> \n" +
                    "</html>";

    private static final String template3 =
            "<!DOCTYPE html> \n" +
                    "            <html> \n" +
                    "            <head> \n" +
                    "               <meta charset=\\utf-8\\> \n" +
                    "               <title>网上报销复核提醒</title> \n" +
                    "         <style type=\"text/css\">\n" +
                    "          .box{ \n" +
                    "            font-size: 20px; \n" +
                    "            color:#000000; \n" +
                    "            margin: 10px;\n" +
                    "          }  \n" +
                    "        </style>\n" +
                    "</head>           \n" +
                    "<body> \n" +
                    " <div>\n" +
                    "　　  <span class=\"box\" style=\"position:absolute; left:10px; top:5px\">\n" +
                    "        <i>\n" +
                    "        网上报销复核提醒：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;+currentDate+报销单据的审批工作已完成，请您在23到24号按如下方式进行单据复核。<br><br>\n" +
                    "        具体审批方式如下：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 1、手机端扫码下图二维码，关注沈阳东睿科技，进入网上报销，点击报销审批<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 2、电脑端访问地址：http://imp.neuray.cn<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 账号为公司邮箱前缀，初始密码为123456，登录后请及时修改。<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 例：邮箱地址zhangs@neuray.cn，登录账号zhangs<br><br>\n" +
                    "        <img src=\"\" alt='img' >\n" +
                    "        </i>\n" +
                    "      </span>\n" +

                    "  </div>\n" +
                    " </body> \n" +
                    "</html>";

    private static final String template4 =
            "<!DOCTYPE html> \n" +
                    "            <html> \n" +
                    "            <head> \n" +
                    "               <meta charset=\\utf-8\\> \n" +
                    "               <title>网上报销复核提醒</title> \n" +
                    "         <style type=\"text/css\">\n" +
                    "          .box{ \n" +
                    "            font-size: 20px; \n" +
                    "            color:#000000; \n" +
                    "            margin: 10px;\n" +
                    "          }  \n" +
                    "        </style>\n" +
                    "</head>           \n" +
                    "<body> \n" +
                    " <div>\n" +
                    "　　  <span class=\"box\" style=\"position:absolute; left:10px; top:5px\">\n" +
                    "        <i>\n" +
                    "        网上报销复核提醒：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;+currentDate+报销单据的审批及财务复核工作已完成，请您在25号按如下方式跟踪单据的打款状态。<br><br>\n" +
                    "        具体审批方式如下：<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 1、手机端扫码下图二维码，关注沈阳东睿科技，进入网上报销，点击报销审批<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp; 2、电脑端访问地址：http://imp.neuray.cn<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 账号为公司邮箱前缀，初始密码为123456，登录后请及时修改。<br><br>\n" +
                    "        &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 例：邮箱地址zhangs@neuray.cn，登录账号zhangs<br><br>\n" +
                    "        <img src=\"\" alt='img' >\n" +
                    "        </i>\n" +
                    "      </span>\n" +

                    "  </div>\n" +
                    " </body> \n" +
                    "</html>";

    /**
     * 每月15日提醒一级审批人
     */
    public void firstApproverStart(){
        // 查询有审批任务的一级审批人
        List<String> stringList = mailReminderMapper.queryFirstApproverMail();
        if(stringList !=null && stringList.size()> 0){
            addMail(stringList,template1,"网上报销审批提醒");
        }
    }

    /**
     * 每月19日提醒二级审批人
     */
    public void secondApproverStart(){
        // 查询有审批任务的二级审批人
        List<String> stringList = mailReminderMapper.querySecondApproverMail();
        if(stringList !=null && stringList.size()> 0){
            addMail(stringList,template1,"网上报销审批提醒");
        }
    }

    /**
     * 每月18日提醒一级审批人
     */
    public void firstApproverEnd(){
        // 查询有审批任务的一级审批人
        List<String> stringList = mailReminderMapper.queryFirstApproverMail();
        if(stringList !=null && stringList.size()> 0){
            addMail(stringList,template2,"网上报销审批提醒");
        }
    }

    /**
     * 每月24日提醒二级审批人
     */
    public void secondApproverEnd(){
        // 查询有审批任务的二级审批人
        List<String> stringList = mailReminderMapper.querySecondApproverMail();
        if(stringList !=null && stringList.size()> 0){
            addMail(stringList,template2,"网上报销审批提醒");
        }
    }

    /**
     * 每月24日提醒三级审批人
     */
    public void thirdApprover(){
        // 查询三级审批人
        List<String> stringList = mailReminderMapper.queryApproverMail("3");
        if(stringList !=null && stringList.size()> 0){
            String model = template3;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            addMail(stringList,model.replace("+currentDate+",sdf.format(new Date())),"网上报销复核提醒");
        }
    }

    /**
     * 每月25日提醒四级审批人
     */
    public void fourthApprover(){
        // 查询四级审批人
        List<String> stringList = mailReminderMapper.queryApproverMail("4");
        if(stringList !=null && stringList.size()> 0){
            String model = template4;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            addMail(stringList,model.replace("+currentDate+",sdf.format(new Date())),"网上报销复核提醒");
        }
    }

    /**
     * 生成模板邮件信息并入库
     * */
    private void addMail(List<String> stringList,String template,String title){
        SysMailSourceVo sysMailSourceVo = new SysMailSourceVo();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 去重去空
        List<String> list = stringList.stream().filter(item-> StringUtils.isNotBlank(item)).distinct().collect(Collectors.toList());

        String content = "";
        ClassPathResource resource1 = null;
        try {
            resource1 = new ClassPathResource("birthpic/qrCode.jpg");
        } catch (Exception e) {
            resource1 = new ClassPathResource("birthpic/qrCode.jpg");
        }
        try {
            String jpg = EmailCommon.fileToBytesToBase64(resource1.getInputStream());
            String model = template;
            content = model.replace("+backPic+",jpg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sysMailSourceVo.setTitle(title);
        sysMailSourceVo.setYjnrType("1");
        sysMailSourceVo.setYjnrBl(content);
        sysMailSourceVo.setYjsj(sdf.format(new Date()));
        sysMailSourceVo.setMailname("neuray");
        sysMailSourceVo.setFjrbm("neuray");
        String sjr = "";
        EntityUtils.setCreatAndUpdatInfo(sysMailSourceVo);
        for(String mail:list){
            sysMailSourceVo.setSjr(mail);
            sysMailSourceVo.setYjnrBl(content);
            sysMailSourceVo.setId(UUIDUtils.generateShortUuid());
            try {
                mailBiz.add(sysMailSourceVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

